﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Data;

namespace prjCompras
{
    public partial class frmRelRequisicao : Form
    {
        public frmRelRequisicao()
        {
            InitializeComponent();
        }

        private void chckbxData_Click(object sender, EventArgs e)
        {
            grpbxData.Enabled = chckbxData.Checked;
        }

        private void chckbxUrgente_Click(object sender, EventArgs e)
        {
            if (chckbxUrgente.CheckState == CheckState.Checked)
            {
                chckbxUrgente.CheckState = CheckState.Unchecked;
            }
            else if (chckbxUrgente.CheckState == CheckState.Unchecked)
            {
                chckbxUrgente.CheckState = CheckState.Indeterminate;
            }
            else
            {
                chckbxUrgente.CheckState = CheckState.Checked;
            }
        }

        private void btnGerar_Click(object sender, EventArgs e)
        {
            string sql = @"Select Codigo, 
                                  Data, 
                                  Case when Situacao = 0 then 'Não Analisada'
                                       when Situacao = 1 then 'Aprovada' 
                                       else 'Reprovada' end Situacao,
                                  case when Urgente = 1 then 'Sim'
                                       else 'Não' end Urgente,
                                  case when Tipo = 0 then 'Requisicao'
                                       else 'Plano de Compras' end Tipo 
                             from Requisicao
                            where (data between @d1 and @d2 or @d1 = '')
                              and (Urgente = @u or @u = '')
                              and (Situacao = @s or @s = '')
                              and (Tipo = @t or @t = '')";
            SqlDataAdapter dt = Conexao.getInstance.executeQueryRelatorio(sql);

            string d1 = "";
            string d2 = "";
            string u = "";
            string s = "";
            string t = "";

            if (chckbxData.Checked)
            {
                d1 = dtmpckerDataInicio.Value.ToShortDateString();
                d2 = dtmpckerDataFim.Value.ToShortDateString();
            }

            if (chckbxUrgente.CheckState != CheckState.Indeterminate)
            {
                if (chckbxUrgente.Checked)
                {
                    u = "1";
                }
                else
                {
                    u = "0";
                }
            }

            if (cmbxSituacao.SelectedIndex != 3)
            {
                s = cmbxSituacao.SelectedIndex.ToString();
            }
            if (cmbxTipo.SelectedIndex != 2)
            {
                t = cmbxTipo.SelectedIndex.ToString();
            }

            dt.SelectCommand.Parameters.Add(new SqlParameter("@d1", d1));
            dt.SelectCommand.Parameters.Add(new SqlParameter("@d2", d2));
            dt.SelectCommand.Parameters.Add(new SqlParameter("@u", u));
            dt.SelectCommand.Parameters.Add(new SqlParameter("@s", s));
            dt.SelectCommand.Parameters.Add(new SqlParameter("@t", t));
            dtstRequisicao dt_req = new dtstRequisicao();
            dt.Fill(dt_req, "Requisicao");

            sql = SqlCrud.getInstance.selectAll("RequisicaoMaterial");
            dt = Conexao.getInstance.executeQueryRelatorio(sql);
            dt.Fill(dt_req, "RequisicaoMaterial");

            sql = SqlCrud.getInstance.selectAll("Material");
            dt = Conexao.getInstance.executeQueryRelatorio(sql);
            dt.Fill(dt_req, "Material");

            rptRequisicao rel = new rptRequisicao();
            rel.SetDataSource(dt_req);
            frmRelatorio f = new frmRelatorio();
            f.Text = "Relatório de Requisições";
            f.crystalReportViewer1.ReportSource = rel;
            f.ShowDialog();
        }

        private void dtmpckerDataInicio_ValueChanged(object sender, EventArgs e)
        {
            dtmpckerDataFim.MinDate = dtmpckerDataInicio.Value;
        }

        private void frmRelRequisicao_Load(object sender, EventArgs e)
        {
            cmbxSituacao.SelectedIndex = 3;
            cmbxTipo.SelectedIndex = 2;
            dtmpckerDataInicio.Value = DateTime.Now;
        }
    }
}
